//
//  TopViewController.m
//  项目一TimeMovie
//
//  Created by apple on 16/3/29.
//  Copyright © 2016年 huangmingyu. All rights reserved.
//
#import "Top250Cell.h"
#import "TopModel.h"
#import "TopViewController.h"
#import "pingjiaController.h"


#define kSpaceSize  (kScreenWidth-kItemWidth*kItemCount)/(kItemCount+1)
#define kItemCount  3       //每行item的个数
#define kItemWidth  100     //item的宽度
#define kItemHeight 166     //item的高度

@interface TopViewController () {
    
    NSMutableArray *dataArray;
}


@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *topCollectionFlowLAyout;

@property (weak, nonatomic) IBOutlet UICollectionView *topCollectionView;

@end

@implementation TopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"Top250";
    
    [self loadData];

    [self configLayout];
    [_topCollectionView registerNib:[UINib nibWithNibName:@"Top250Cell" bundle:nil] forCellWithReuseIdentifier:@"12345"];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)configLayout
{
    self.topCollectionFlowLAyout.itemSize = CGSizeMake(kItemWidth, kItemHeight);
    self.topCollectionFlowLAyout.minimumLineSpacing = kSpaceSize;
    self.topCollectionFlowLAyout.minimumInteritemSpacing = kSpaceSize;
    self.topCollectionFlowLAyout.sectionInset = UIEdgeInsetsMake(kSpaceSize, kSpaceSize, kSpaceSize, kSpaceSize);
}

- (void)loadData {
    
    dataArray = [NSMutableArray array];
    
    
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"top250.json" ofType:nil]] options:NSJSONReadingMutableContainers error:nil];
    NSArray *array = dict[@"subjects"];
    for (NSDictionary *dic in array) {
        
        TopModel *model = [[TopModel alloc] init];
//        @property (nonatomic, copy) NSString *title;
//        @property (nonatomic, copy) NSDictionary *imageName;
//        @property (nonatomic, strong) NSDictionary *rating;
        
        model.title = dic[@"title"];
        
        model.imageName = dic[@"images"];
        
        model.rating = dic[@"rating"];
        
        [dataArray addObject:model];
        
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    Top250Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"12345" forIndexPath:indexPath];
    
    cell.model = dataArray[indexPath.row];
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return dataArray.count;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    pingjiaController *pjContrl = [self.storyboard instantiateViewControllerWithIdentifier:@"pingjiaController"];
    
    [self.navigationController pushViewController:pjContrl animated:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
